/*     */ package com.newcrm.db;
/*     */ 
/*     */ import com.newcrm.exception.DBException;
/*     */ import java.sql.Connection;
/*     */ import java.sql.SQLException;
/*     */ import java.text.MessageFormat;
/*     */ import java.util.ArrayList;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import org.apache.commons.dbutils.DbUtils;
/*     */ import org.apache.commons.dbutils.MyMapListHandler;
/*     */ import org.apache.commons.dbutils.QueryRunner;
/*     */ import org.apache.commons.dbutils.handlers.ScalarHandler;
/*     */ import org.apache.log4j.Logger;
/*     */ 
/*     */ public abstract class AbstractDBManager
/*     */ {
/*  20 */   private static Logger logger = Logger.getLogger(AbstractDBManager.class
/*  21 */     .getName());
/*     */ 
/*     */   protected int getTotalNums(String sql, List<Object> params)
/*     */   {
/*  32 */     Connection conn = ConnectionManager.getInstance().getConnection();
/*  33 */     if (conn == null) {
/*  34 */       return 0;
/*     */     }
/*     */ 
/*  37 */     QueryRunner runner = new QueryRunner();
/*     */     try {
/*  39 */       Object obj = runner.query(conn, sql, new ScalarHandler(1), 
/*  40 */         params.toArray());
/*  41 */       if (!(obj instanceof Long)) {
/*  42 */         return 0;
/*     */       }
/*  44 */       return ((Long)obj).intValue();
/*     */     } catch (SQLException e) {
/*  46 */       logger.warn("get count failed", e);
/*     */     } finally {
/*     */       try {
/*  49 */         DbUtils.close(conn);
/*     */       } catch (SQLException e) {
/*  51 */         logger.warn("close conn failed", e);
/*     */       }
/*     */     }
/*     */ 
/*  55 */     return 0;
/*     */   }
/*     */ 
/*     */   protected List<Map<String, Object>> searchRecords(String sql, List<Object> params, int offset, int pageSize) throws DBException
/*     */   {
/*  60 */     Connection conn = ConnectionManager.getInstance().getConnection();
/*  61 */     if (conn == null) {
/*  62 */       return null;
/*     */     }
/*     */ 
/*  65 */     QueryRunner runner = new QueryRunner();
/*     */     try {
/*  67 */       sql = sql + " LIMIT " + offset + ", " + pageSize;
/*  68 */       return (List)runner.query(conn, sql, new MyMapListHandler(), 
/*  69 */         params.toArray());
/*     */     } catch (SQLException e) {
/*  71 */       throw new DBException();
/*     */     } finally {
/*     */       try {
/*  74 */         DbUtils.close(conn);
/*     */       } catch (SQLException e) {
/*  76 */         logger.warn("close conn failed", e);
/*     */       }
/*     */     }
/*     */   }
/*     */ 
/*     */   protected List<Integer> deleteByIds(String deletedIds, String sql) throws DBException
/*     */   {
/*  83 */     List clearIds = new ArrayList();
/*  84 */     if ((deletedIds == null) || (deletedIds.length() == 0)) {
/*  85 */       return clearIds;
/*     */     }
/*  87 */     Connection conn = ConnectionManager.getInstance().getConnection();
/*  88 */     if (conn == null) {
/*  89 */       throw new DBException();
/*     */     }
/*     */ 
/*  92 */     QueryRunner runner = new QueryRunner();
/*     */     try {
/*  94 */       String[] ids = deletedIds.split(",");
/*  95 */       for (String id : ids)
/*  96 */         if ((id != null) && (id.trim().length() != 0))
/*     */         {
/*  99 */           int deleteId = 0;
/*     */           try {
/* 101 */             deleteId = Integer.parseInt(id.trim());
/*     */           } catch (NumberFormatException e) {
/* 103 */             continue;
/*     */           }
/*     */           try {
/* 106 */             int result = runner.update(conn, sql, Integer.valueOf(deleteId));
/* 107 */             if (result >= 1)
/* 108 */               clearIds.add(Integer.valueOf(deleteId));
/*     */           }
/*     */           catch (SQLException localSQLException1)
/*     */           {
/*     */           }
/*     */         }
/* 114 */       return clearIds;
/*     */     } finally {
/*     */       try {
/* 117 */         DbUtils.close(conn);
/*     */       } catch (SQLException e) {
/* 119 */         logger.warn("close conn failed", e);
/*     */       }
/*     */     }
/*     */   }
/*     */ 
/*     */   protected String getSQL(String sql, String idx) {
/* 125 */     if (idx == null) {
/* 126 */       idx = "";
/*     */     }
/* 128 */     return MessageFormat.format(sql, new Object[] { idx });
/*     */   }
/*     */ 
/*     */   protected String getSQL(String sql, String idx, String field) {
/* 132 */     if (idx == null) {
/* 133 */       idx = "";
/*     */     }
/* 135 */     return MessageFormat.format(sql, new Object[] { idx, field });
/*     */   }
/*     */ }

/* Location:           C:\E\referenceProject\CRM参考\qikebao\WEB-INF\lib\qikebao.jar
 * Qualified Name:     com.newcrm.db.AbstractDBManager
 * JD-Core Version:    0.6.1
 */